<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
$isSpecial = (int)$_data["isSpecial"];
$intSpecialNumType = (int)$_data["SpecialNumType"];
$isRealShow = (int)$_data["isRealShow"];
$intGambleID = (int)$_data["GambleID"];
if(strlen($isSpecial) == 0)$isSpecial = 0;
if(strlen($isRealShow) != 1)$isRealShow = 0;
$strBT = trim($_data["Bet_Type"]);
$arrBType = explode(",", $strBT);
$arrOtherMsg = explode(",", trim($_data["OtherMsg"]));

if($strBT == "14"){
	$strSQL = "select a.Bet_Type as Bet_Type, a.Number as Number, SUM(b.MemStake) as tolStake, SUM(b.MemPayOff) as tolPayOff, SUM(b.MemStake*MemExchangeRate*DirectorRate) as tolRealStake, SUM(b.MemPayOff*MemExchangeRate*DirectorRate) as tolRealPayOff, COUNT(*) as tolWager, SUM(b.MemStake*MemExchangeRate*DirectorRate*DirectorCommission) as tolCommission  from `bet_ParlaysNumber` a, `bet_BetListTmp` b where a.BetListID = b.ID and b.GambleID = '".$intGambleID."' group by a.Bet_Type, a.Number";
	if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
		$GLOBALS["errormessage"] = "Find TodayGame Data Error!!";
		$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
		ErrMsgOutputHTML("Error", "", 1);
	}
	while($Rows = PMA_mysql_fetch_object($RS)){
		$arrBet[$Rows -> Bet_Type][$Rows -> Number] = $Rows;
	}
		
	$strSQL = "select * from `bet_TodayGame".$intLID."` where LottoID = ".$intLID." and GambleID = '".$intGambleID."' and Bet_Type in (11, 12, 13) order by Bet_Type";

}else{
	$strSQL = "select * from `bet_TodayGame".$intLID."` where LottoID = ".$intLID." and GambleID = '".$intGambleID."' and Bet_Type in (".$strBT.") order by Bet_Type, Number";
}

if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Find TodayGame Data Error!!";
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

if(PMA_mysql_num_rows($RS) == 0){
	$GLOBALS["errormessage"] = "No TodayGame Data!!";
	//$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
	
$arrSumStake = array();
$arrSumPayOff = array();
$arrSumCommission = array();
$arrPreWin = array();
$arrCon = array();
while($Rows = PMA_mysql_fetch_object($RS)){
	if($isRealShow == 0){
		if($strBT == "14"){
			$tolStake = $arrBet[$Rows -> Bet_Type][$Rows -> Number] -> tolStake;
			$tolPayOff = $arrBet[$Rows -> Bet_Type][$Rows -> Number] -> tolPayOff;
			$tolWager = $arrBet[$Rows -> Bet_Type][$Rows -> Number] -> tolWager;
		}else{
			$tolStake = $Rows -> tolStake;
			$tolPayOff = $Rows -> tolPayOff;
			$tolWager = $Rows -> tolWager;
		}
		$tolCommission = 0;
	}else{
		if($strBT == "14"){
			$tolStake = $arrBet[$Rows -> Bet_Type][$Rows -> Number] -> tolRealStake;
			$tolPayOff = $arrBet[$Rows -> Bet_Type][$Rows -> Number] -> tolRealPayOff;
			$tolWager = $arrBet[$Rows -> Bet_Type][$Rows -> Number] -> tolWager;
			$tolCommission = $arrBet[$Rows -> Bet_Type][$Rows -> Number] -> tolCommission;
		}else{
			$tolStake = $Rows -> tolRealStake;
			$tolPayOff = $Rows -> tolRealPayOff;
			$tolWager = $Rows -> tolWager;
			$tolCommission = $Rows -> tolCommission;
		}
	}
	if(empty($tolStake))$tolStake = 0;
	if(empty($tolPayOff))$tolPayOff = 0;
	if(empty($tolCommission))$tolCommission = 0;
	if(empty($tolWager))$tolWager = 0;
	if($strBT != "14"){
		$tolStake -= $Rows -> tolSuppleStake;
		$tolPayOff -= $Rows -> tolSupplePayOff;
		$tolCommission -= $Rows -> tolSuppleCommission;
	}
	$intBT = $Rows -> Bet_Type;
	$intApointNum = $_arrGLineNumToApointNum[$Rows -> LottoID][$Rows -> Bet_Type][$Rows -> Number];
	if($intApointNum == null)$intApointNum = 0;
	$strTmpCon = "parent.Game".$intBT."[".$Rows -> Number."]=new Array('".$Rows -> LottoID."','".$Rows -> GambleID."','".$intBT."','".$Rows -> Number."','".$Rows -> OddsID."','".round(($Rows -> Odds+$_arrGambleType[$Rows -> Bet_Type][$intApointNum]), 3)."','".$Rows -> isStopBet."','".$tolWager."','".$tolStake."','".$tolPayOff."','".$tolCommission."');\n";

	$strCon .= $strTmpCon;
	switch($Rows -> Bet_Type){
		case 11:
		case 12:
		case 13:
			$intNum = floor($Rows -> Number / 10);
			$arrSumStake[$Rows -> Bet_Type][$intNum] += $tolStake;
			$arrSumPayOff[$Rows -> Bet_Type][$intNum] += $tolPayOff;
			$arrSumCommission[$Rows -> Bet_Type][$intNum] += $tolCommission;
			break;

		default:
			$arrSumStake[$Rows -> Bet_Type] += $tolStake;
			$arrSumPayOff[$Rows -> Bet_Type] += $tolPayOff;
			$arrSumCommission[$Rows -> Bet_Type] += $tolCommission;
			$sumWStake[$Rows -> Bet_Type] += $tolStake + $Rows -> tolSuppleStake;

			if($arrOtherMsg[0] == "AllSort"){
				$arrCon[$Rows -> Bet_Type][$Rows -> Number] = $strTmpCon;
				$arrPreWin[$Rows -> Bet_Type][$Rows -> Number] = $tolStake + $tolPayOff;
			}
			break;
	}
}

$arrColorNum = array("", "", "", "");
if($arrOtherMsg[0] == "AllSort"){
	$strCon = "";
	$arrMaxNumNo = array();
	foreach($arrCon as $intBT => $arrC){
		$arrPreW = $arrPreWin[$intBT];

		$arrSpecialNum = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
		$arrSpecialCon = array("", "", "", "", "", "", "", "", "", "");
		$arrV = array(-1000000000, -1000000000, -1000000000, -1000000000, -1000000000, -1000000000, -1000000000, -1000000000, -1000000000, -1000000000);
		//$intNo = 0;

		foreach($arrPreW as $intNum => $flPreWin){
			$flPreWin = $arrSumStake[$intBT] - $flPreWin - $arrSumCommission[$intBT];
			//$arrC[$intNum] = str_replace("[".$intNum."]", "[".$intNo."]", $arrC[$intNum]);
			$arrV = getValueSort($arrV, $flPreWin*-1, $intNum, $arrC[$intNum]);
			//$intNo++;
		}
		foreach($arrSpecialNum as $k => $intNum){
			$arrSpecialCon[$k] = str_replace("[".$intNum."]", "[".$k."]", $arrSpecialCon[$k]);
			$arrMaxNumNo[$intNum]++;
		}

		$strCon .= implode("", $arrSpecialCon);
	}
	arsort($arrMaxNumNo);
	foreach($arrMaxNumNo as $intNum => $intNo){
		switch($intNo){
			case 10:
			case 9:
				$arrColorNum[0] .= $intNum.",";
				break;
			
			case 8:
				$arrColorNum[1] .= $intNum.",";
				break;

			case 7:
				$arrColorNum[2] .= $intNum.",";
				break;

			case 6:
				$arrColorNum[3] .= $intNum.",";
				break;
		}
	}
}

function getValueSort($arrValue, $intValue, $intNum, $strCon){
	global $arrSpecialNum, $arrSpecialCon;
	$i = 0;
	$intTmp = 0;
	$intTmp2 = 0;
	$isMoveDown = false;
	$strCon1 = "";
	$strCon2 = "";
	$intNum1 = 0;
	$intNum2 = 0;

	for($i = 0; $i < sizeof($arrValue); $i++){
		if($isMoveDown == false){
			if($arrValue[$i] < $intValue){
				$intTmp = $arrValue[$i];
				$intNum1 = $arrSpecialNum[$i];
				$strCon1 = $arrSpecialCon[$i];
				$arrValue[$i] = $intValue;
				$arrSpecialNum[$i] = $intNum;
				$arrSpecialCon[$i] = $strCon;
				$isMoveDown = true;
			}
		}else{
			if($isMoveDown == true){
				$intTmp2 = $arrValue[$i];
				$intNum2 = $arrSpecialNum[$i];
				$strCon2 = $arrSpecialCon[$i];
				$arrValue[$i] = $intTmp;
				$arrSpecialNum[$i] = $intNum1;
				$arrSpecialCon[$i] = $strCon1;
				$intTmp = $intTmp2;
				$intNum1 = $intNum2;
				$strCon1 = $strCon2;
			}
		}
	}
	return $arrValue;
}
/*=================================================================*/
header("Cache-Control: no-store, no-cache, must-revalidate");	  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");															  // HTTP/1.0
?><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript">
<!--
<?php 
print $strCon."\n";
print "parent.isEmergency = ".((empty($_isStopAllBet))?"0":$_isStopAllBet).";\n";
print "parent.changEmergency();\n";
print "parent.arrColorNumber[0] = '@@,".$arrColorNum[0]."';\n";
print "parent.arrColorNumber[1] = '@@,".$arrColorNum[1]."';\n";
print "parent.arrColorNumber[2] = '@@,".$arrColorNum[2]."';\n";
print "parent.arrColorNumber[3] = '@@,".$arrColorNum[3]."';\n";

foreach($arrSumStake as $intBT => $val){
	switch($intBT){
		case 11:
		case 12:
		case 13:
			print "parent.sumStake[".$intBT."] = new Array(0, ".(int)round($arrSumStake[$intBT][1]).", ".(int)round($arrSumStake[$intBT][2]).", ".(int)round($arrSumStake[$intBT][3]).", ".(int)round($arrSumStake[$intBT][4]).");\n";
			print "parent.sumPayOff[".$intBT."] = new Array(0, ".(int)round($arrSumPayOff[$intBT][1]).", ".(int)round($arrSumPayOff[$intBT][2]).", ".(int)round($arrSumPayOff[$intBT][3]).", ".(int)round($arrSumPayOff[$intBT][4]).");\n";
			print "parent.sumCommission[".$intBT."] = new Array(0, ".(int)round($arrSumCommission[$intBT][1]).", ".(int)round($arrSumCommission[$intBT][2]).", ".(int)round($arrSumCommission[$intBT][3]).", ".(int)round($arrSumCommission[$intBT][4]).");\n";
			break;

		default:
			print "parent.sumStake[".$intBT."] = ".(int)round($arrSumStake[$intBT]).";\n";
			print "parent.sumPayOff[".$intBT."] = ".(int)round($arrSumPayOff[$intBT]).";\n";
			print "parent.sumCommission[".$intBT."] = ".(int)round($arrSumCommission[$intBT]).";\n";
			print "parent.sumWStake[".$intBT."] = ".(int)round($sumWStake[$intBT]).";\n";
			break;
	}
}

if($arrOtherMsg[0] == "AllSort"){
	switch($strBT){
		case "1":
		case "5":
		case "6":
		case "7":
		case "8":
		case "29":
		case "1,5,6,7,8,29":
			print "parent.ShowBT1NumberSort(1);\n";
			break;

		case "15":
		case "16":
		case "17":
		case "18":
		case "19":
		case "20":
		case "21":
		case "22":
		case "23":
		case "24":
		case "15,16,17,18,19,20,21,22,23,24":
			print "parent.ShowBT1NumberSort(15);\n";
			break;

		case "25":
		case "26":
		case "27":
		case "28":
		case "25,26,27,28":
			print "parent.ShowBT1NumberSort(25);\n";
			break;
	}

}else{ // if($arrOtherMsg[0] == "AllSort"){
	switch($strBT){
		case "1,5,6,7,8,29":
			print "parent.ShowBt1NumberSort(1);\n";
			break;

		case "1":
		case "2":
		case "3":
		case "4":
		case "1,2,3,4":
			print "parent.ShowNumberOdds(1);\n";
			break;

		case "15":
		case "16":
		case "17":
		case "18":
		case "19":
		case "20":
		case "21":
		case "22":
		case "23":
		case "24":
			print "parent.ShowJoinNumberOdds(".$strBT.");\n";
			break;

		case "9":
		case "10":
		case "11":
		case "12":
		case "13":
		case "9,10,11,12,13":
			print "parent.ShowNormalNumber1_4Odds(0);\n";
			break;

		case "14":
			print "parent.ShowNormalNumber1_4Odds(1);\n";
			break;

		case "29":
			print "parent.ShowMantissaOdds();\n";
			break;

		case "5":
		case "6":
		case "7":
		case "8":
			print "parent.ShowNumberOdds(".$strBT.");\n";
			break;

		case "25":
		case "26":
		case "27":
		case "28":
			print "parent.ShowFiveNotWinNumber(".$strBT.");\n";
			break;

		case "30":
		case "31":
		case "32":
		case "33":
		case "34":
		case "35":
		case "30,31,32,33,34,35":
			print "parent.ShowBT3035Number(30);\n";
			break;
	}
} // if($arrOtherMsg[0] == "AllSort"){
?>
//-->
</script>